package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.AnalysisIp;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.AnalysisIpDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2018-11-26 15:04:56
 */
public class AnalysisIpConverts {

    private static Logger LOG = LoggerFactory.getLogger(AnalysisIpConverts.class);

    public static List<AnalysisIpDTO> convert(List<AnalysisIp> analysisIpList) {
        if (CollectionUtils.isEmpty(analysisIpList)) {
            return Collections.EMPTY_LIST;
        }

        List<AnalysisIpDTO> analysisIpListResult = Lists.newArrayList();
        for (AnalysisIp analysisIp : analysisIpList) {
            analysisIpListResult.add(convert(analysisIp));
        }
        return analysisIpListResult;
    }

    public static AnalysisIpDTO convert(AnalysisIp analysisIp) {
        try {
            return BeanUtils.copy(analysisIp, AnalysisIpDTO.class);
        } catch (Exception e) {
            LOG.error("AnalysisIp到AnalysisIpDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisIp到AnalysisIpDTO转换失败", e);
        }
    }

    public static AnalysisIp convert(AnalysisIpDTO analysisIpDTO) {
        try {
            return BeanUtils.copy(analysisIpDTO, AnalysisIp.class);
        } catch (Exception e) {
            LOG.error("AnalysisIpDTO到AnalysisIp转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisIpDTO到AnalysisIp转换失败", e);
        }
    }
}